package com.heima.wemedia.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.wemedia.mapper.WmChannelMapper;
import com.heima.wemedia.service.WmChannelService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

//<<<<<<< HEAD
//import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
//import com.heima.model.wemedia.pojos.WmChannel;
//import com.heima.wemedia.mapper.WmChannelMapper;
//import com.heima.wemedia.service.WmChannelService;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.stereotype.Service;
//import org.springframework.transaction.annotation.Transactional;
//
//=======
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.admin.dtos.AdChannelDto;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.wemedia.mapper.WmChannelMapper;
import com.heima.wemedia.mapper.WmNewsMapper;
import com.heima.wemedia.service.WmChannelService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;


@Service
@Transactional
@Slf4j
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel> implements WmChannelService {


@Autowired
WmChannelMapper wmChannelMapper;
@Autowired
    WmNewsMapper wmNewsMapper;
    //查询所有
    @Override
    public ResponseResult findAll(AdChannelDto channelDto) {

        IPage page = new Page(channelDto.getPage(),channelDto.getSize());
        LambdaQueryWrapper<WmChannel> lqw = new LambdaQueryWrapper<>();
            //如果传过来的名字为不空，那么查询所有的频道并分页展示
            WmChannel wmChannel = new WmChannel();
             lqw.like(channelDto.getName()!=null,WmChannel::getName, channelDto.getName());
            //从数据库中把数据拿出来都放进去
        lqw.eq(channelDto.isStatus(),WmChannel::getStatus,channelDto.isStatus());
        lqw.orderByDesc(WmChannel::getCreatedTime);
        page(page,lqw);
        ResponseResult result = new PageResponseResult(channelDto.getPage(),channelDto.getSize(),(int)page.getTotal());
       result.setData(page.getRecords());
        return result;
    }
    //修改的频道
    @Override
    public ResponseResult updateChannel(WmChannel wmChannel) {
        //首先查询那边文章表是否有相关频道下的文章，如果有那么就需要改文章的频道
        //狀態：狀態為被引用則不能禁用-->wmnews表裏的頻道id，去那個裏面查，如果有就打滅
        if(!StringUtils.isEmpty(wmChannel.getName()) ){
            //傳過來的參數，name和描述不能為空
            LambdaQueryWrapper<WmNews> lqw = new LambdaQueryWrapper<>();
            //從文章表找相應的頻道id
        if(!wmChannel.getStatus()){
            //這裏是傳下來的狀態要改為false也就是禁用的話
            //那判斷是否引用
            lqw.eq(WmNews::getChannelId,wmChannel.getId());
            if (wmNewsMapper.selectList(lqw).size()>0){
                return ResponseResult.errorResult(AppHttpCodeEnum.CHANNEL_CITED,"頻道被引用，無法修改");
            }
        }
            wmChannelMapper.updateById(wmChannel);
            return  ResponseResult.okResult("修改頻道成功");
        }

        return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "相關信息不能爲空");
    }
    //新建频道，因为新建频道，所以文章不用关联的，后面发布的文章关联频道是他们自己的事
    @Override
    public ResponseResult saveChannel(WmChannel wmChannel) {

        //频道名不能为空
        if (wmChannel.getName()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道名不能为空");
        }//频道名不为null时，查询是否有一样的频道名。
        LambdaQueryWrapper<WmChannel> lqw = new LambdaQueryWrapper<>();
        lqw.eq(wmChannel.getName()!=null,WmChannel::getName,wmChannel.getName());
        List<WmChannel> wmChannels = wmChannelMapper.selectList(lqw);
        if (wmChannels.size() != 0){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道名不能相同");
        }
        //把传过来的对象没有的东西加上
        wmChannel.setCreatedTime(new Date());
        wmChannel.setIsDefault(true);
        wmChannelMapper.insert(wmChannel);
        return ResponseResult.okResult("添加频道成功");
    }
    //刪除頻道
    @Override
    public ResponseResult deleteChannel(Integer id) {
        //只有被禁用的頻道才能被刪除
        WmChannel wmChannel = getOne(Wrappers.<WmChannel>lambdaQuery().eq(WmChannel::getId, id));
        log.info("--{}",wmChannel.getId());
        //狀態為啓用就不能刪除
        if (wmChannel.getStatus()){
            return ResponseResult.errorResult(AppHttpCodeEnum.CHANNEL_ACTIVE);
        }
        return ResponseResult.okResult( wmChannelMapper.deleteById(wmChannel.getId()));
    }
}